package org.jeecg.modules.third.jg.report.freight.information.util.aes;

import org.apache.commons.codec.binary.Hex;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.util.Objects;

/**
 * AES
 */
public final class ShaanxiAESUtil {

    private ShaanxiAESUtil() {

    }
    private static final String AES = "AES";
    private static final String CHARSET = "UTF-8";
    private static final String AES_CBC_PKCS5PADDING = "AES/CBC/PKCS5Padding";
    private static final int AES_128 = 128;

    /**
     * 加密或解密
     * @param mode 加解密标识
     * @param byteContent 处理内容
     * @param keys 密钥
     * @param iv 偏移量
     * @return byte[]
     * @throws Exception 异常
     */
    private static byte[] encryptOrDecrypt(int mode, byte[] byteContent, byte[] keys, byte[] iv) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(keys, AES);
        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING);
        if (null != iv) {
            cipher.init(mode, keySpec, new IvParameterSpec(iv));
        } else {
            cipher.init(mode, keySpec);
        }
        byte[] result = cipher.doFinal(byteContent);
        return result;
    }

    /**
     * 生成SecureRandom方式key
     * @param key 密钥
     * @return byte[]
     */
    public static byte[] generateKey(String key) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance(AES);
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(key.getBytes());
            kgen.init(AES_128, random);
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();

            return enCodeFormat;
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 加密
     * @param source 数据
     * @param key 密钥
     * @param iv 偏移量
     * @return byte[]
     */
    private static byte[] encrypt(byte[] source, byte[] key, byte[] iv) {
        byte[] encodeByte = null;
        try {
            encodeByte = encryptOrDecrypt(Cipher.ENCRYPT_MODE, source, key, iv);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encodeByte;
    }

    /**
     * 加密
     * @param source 数据
     * @param key 密钥
     * @param iv 偏移量
     * @return String HEX
     */
    public static String encryptHex(String source, String key, String iv) {
        String encodeStr = "";
        try {
            byte[] encodeByte = encrypt(source.getBytes(CHARSET), generateKey(key), iv.getBytes(CHARSET));
            if (!Objects.isNull(encodeByte)) {
                encodeStr = Hex.encodeHexString(encodeByte);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encodeStr;
    }
}
